package com.shenyong.ladmin.service.Impl;

import com.shenyong.controller.BaseController;
import com.shenyong.entity.LoginUsers;
import com.shenyong.exception.MyException;
import com.shenyong.ladmin.entity.Menu;
import com.shenyong.ladmin.mapper.MenuMapper;
import com.shenyong.ladmin.service.MenuService;
import com.shenyong.service.UserService;
import com.shenyong.util.JwtUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @site
 * @company
 * @create
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private UserService userService;
    @Autowired
    private BaseController baseController;
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> getTreeTable() {
        return menuMapper.getTreeTable();
    }

    public List<Map<String, Object>> getTree(Integer user_id) {
        String nickName1= baseController.getToken();
        String nickName= JwtUtil.getUsername(nickName1);
        LoginUsers user= userService.getName(nickName);
        int userid=user.getUser().getUser_id();
        user_id=userid;
        // TODO Auto-generated method stub
        List<Map<String, Object>> listmap=new ArrayList<Map<String, Object>>();
        List<Menu> mo1=menuMapper.getTree(user_id);
        for (int i = 0; i < mo1.size(); i++) {
            Map<String, Object> map=new HashMap<String, Object>();
            map.put("menu_id", mo1.get(i).getMenu_id());
            map.put("title", mo1.get(i).getMenu_name());
            map.put("menu_menuId", mo1.get(i).getMenu_menuId());
            map.put("menu_url", mo1.get(i).getMenu_url());
            map.put("children", test(user_id,mo1.get(i).getMenu_id()));
            listmap.add(map);
        }
        return listmap;
    }

    @Override
    public int saveTreeTable(Menu menu) {
        return menuMapper.saveTreeTable(menu);
    }

    @Override
    public List<Menu> selectMenuPid(int id) {
        return menuMapper.selectMenuPid(id);
    }

    @Override
    public int deleteTreeTable(int id) {
        if(menuMapper.selectMenuPid(id).size()!=0){
            throw new MyException("此节点下有子节点,不允许删除");
        }
        return menuMapper.deleteTreeTable(id);
    }

    @Override
    public int updateTreeTable(Menu menu) {
        return menuMapper.updateTreeTable(menu);
    }


    public List<HashMap<String, Object>> getTreeOne(List<com.shenyong.ladmin.entity.Menu> menu, List<HashMap<String, Object>> result,  List<Menu> role, Integer id) {
        int i= 0;
        for(com.shenyong.ladmin.entity.Menu m : menu){
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", m.getMenu_id());
            map.put("title", m.getMenu_name());
            map.put("spread", true);      //设置是否展开
            List<HashMap<String, Object>> result1 = new ArrayList<>();
            List<com.shenyong.ladmin.entity.Menu> children=menuMapper.getTreeChildren(1,m.getMenu_id());    //下级菜单
            System.out.println(menuMapper.getTreeOne().get(i).getMenu_name()+"---------------"+m.getMenu_name());
           if(menuMapper.getTreeOne().get(i).getMenu_name().contains(m.getMenu_name())){    //判断该角色之前是否有这个权限菜单
                    map.put("checked", true);    //设置为选中状态
            }
            i++;
            map.put("children", getTreeOne(children, result1, role, id));
            result.add(map);
        }
        return result;
    }
    @Override
    public Object menuload(Integer id){
        List<com.shenyong.ladmin.entity.Menu> menu = menuMapper.getTree(2); //查找所有菜单
        List<HashMap<String, Object>> result = new ArrayList<>();    //定义一个map处理json键名问题
        List<Menu> role = menuMapper.getTreeOne(); //查询这个角色之前有哪些权限菜单
        return getTreeOne(menu, result, role, id);
    }

    public List<Map<String, Object>> test(int user_id,int id) {
        // TODO Auto-generated method stub
		/*modules.setmParentId(uId);*/
        List<Map<String, Object>> listmap=new ArrayList<Map<String, Object>>();
        List<Menu> mo1=menuMapper.getTreeChildren(user_id,id);
        if(mo1!=null){
            for (int i = 0; i < mo1.size(); i++) {
                Map<String, Object> map=new HashMap<String, Object>();
                map.put("menu_id", mo1.get(i).getMenu_id());
                map.put("title", mo1.get(i).getMenu_name());
                map.put("menu_menuId", mo1.get(i).getMenu_menuId());
                map.put("menu_url", mo1.get(i).getMenu_url());
                if(mo1!=null){
                    map.put("children", test(user_id,mo1.get(i).getMenu_id()));
                }
                listmap.add(map);
            }
        }
        return listmap;
    }
}
